package com.cx.singleton;

/**
 * @author zhangshuai
 * @version V1.0
 * @Project Java2_26
 * @Package com.cx.singleton
 * @Description:
 * @Date 2025/5/27 8:44
 */
//使用内部类实现单例模式
public class HolderSingleton {
    //构造函数私有，防止外部创建实例
    private HolderSingleton(){
        System.out.println("HolderSingleton");
    }
    //定义静态内部类，持有单例对象
    private static class Holder{
        private static final HolderSingleton instance = new HolderSingleton();
    }
    //提供静态方法，返回单例对象
    public static HolderSingleton getInstance(){
        return Holder.instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                HolderSingleton instance1 = HolderSingleton.getInstance();
                System.out.println(instance1);
            }).start();
            new Thread(()->{
                HolderSingleton instance1 = HolderSingleton.getInstance();
                System.out.println(instance1);
            }).start();
            new Thread(()->{
                HolderSingleton instance1 = HolderSingleton.getInstance();
                System.out.println(instance1);
            }).start();

        }

    }
}